layui.define(['index', 'zmnApp', 'form', 'zmnSelect'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let layer = layui.layer;
    let zmnApp = layui.zmnApp;
    let zmnSelect = layui.zmnSelect;

    form.render(null, "zmn-form");

    form.on('submit(zmn-form-submit)', function (obj) {
        var data = obj.field;
        if (data.subCompanyId ===null || data.subCompanyId===""){
            layui.layer.msg("请选择子公司");
            return false;
        }
        if (data.subArea ===null || data.subArea===""||data.subArea=="0"){
            layui.layer.msg("请选择所属片区");
            return false;
        }
        if (data.isManager !="2" && data.isManager!="1"){
            layui.layer.msg("请选择是否片区负责人");
            return false;
        }

        let url = "/staff/subarea/addEdit.action";
        $.ajax({
            async: false,
            type: "POST",
            url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            success: function (result) {
                let message = result.status === 200 ? "操作成功！" : result.message;
                layer.msg(message);
                if (result.status === 200) {
                    layer.msg(message, {"icon": 1});
                    setTimeout(function () {
                        zmnLayUtil.closeThisIframe(parent);
                    }, 1000)
                } else {
                    layer.msg(message, {"icon": 2});
                }
                return;
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            }
        });

        // $.post(zmnApp.SYS_BASE_URL + "/staff/subarea/addEdit.action", {
        //     "staffId": staff.staffId,
        //     "bsUserId": staff.bsUserId
        // }, function (res) {
        //     if (res.status === 200) {
        //         layer.msg(res.message, {"icon": 1});
        //         setTimeout(function () {
        //             zmnLayUtil.closeThisIframe(parent);
        //         }, 1000)
        //     } else {
        //         layer.msg(res.message, {"icon": 2});
        //     }
        // });
        return false;
    });

    zmnSelect.selectlPlatCompany('plat', 'subCompanyId', {
        plat: {
            selectedValues: json.vo.plat,
            remote: {
                limitPermit: json.vo.limitPermit,
            },
            events: {
                changed: function (values) {
                    console.log("==========platId21=========");
                    console.log("已选中的值:" + values);
                }
            }
        },
        subCompanyId: {
            isRadio: true,
            selectedValues: json.vo.subCompanyId,
            remote: {
                limitPermit: json.vo.limitPermit,
                events: {
                    extendParams: function () {
                        return {type: 120}
                    }
                },
            },
            events: {
                changed: function (values) {
                    console.log("==========companyId21=========");
                    console.log("已选中的值:" + values);
                }
            }
        }
    })

    //查询条件重置
    form.on('submit(zmn-form-reset)', function (obj) {
        form.val("zmn-form", {
            "subArea": "0",
            "isManager":"0"
        });
        return false;
    });

    //点击取消
    $(document).on('click', '#cancle', function () {
        zmnLayUtil.closeThisIframe(parent);
    });

    //对外暴露的接口
    exports('staff_sub_area', {});
});
